## Universidade Federal do Rio Grande do Sul - Instituto de Informatica
## Departamento de Informatica Teorica
## Modelos de Linguagem de Programacao - 2013/1
## Professor: Lucas Schnorr
## Alunos: Felipe Schimidt e Mario Gasparoni Junior
##
##
## 
## Implementacao do modulo 'background' seguindo recomendacoes em:
## http://www.pygame.org/docs/

import pygame, commons, ammo 
import spoon

class Tower:
	#Tower Images and attrs
	tower_north = pygame.image.load("images/shooter_man1.png")
	tower_south = pygame.image.load("images/shooter_man2.png")
	#spoon = pygame.image.load("images/spoon.png")
	tower_delay = commons.TOWER_DELAY
	fired_spoons = []
	ammunition = 0
	orientation = 'south'
	x_pos = 0
	y_pos = 0

	def __init__(self, x_pos=None, y_pos=None, orientation=None):
		
		self.ammunition = ammo.Ammo()
		if x_pos is not None:
			self.x_pos = x_pos
		if x_pos is not None:
			self.y_pos = y_pos
		if orientation == 'north':
			self.orientation = 'north'

	def track(self, screen):
		player_position = pygame.mouse.get_pos()
		self.x_pos = player_position[0]
		self.y_pos = player_position[1]
		pressed = pygame.key.get_pressed()
		if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
			if self.orientation == 'north':
				self.orientation = 'south'
			else:
				self.orientation = 'north'

		#Coordinates to centralize the tower with the mouse pointer
		if self.orientation == 'north':
			screen.blit(self.tower_north, [self.center_x(),self.center_y()])
		else:
			screen.blit(self.tower_south, [self.center_x(),self.center_y()])

	def update(self, screen, placed_towers):
		for i in range(len(placed_towers)):
			tower = placed_towers[i]
			if tower.orientation == 'north':
				screen.blit(self.tower_north, [tower.center_x(), tower.center_y()])
			else:
				screen.blit(self.tower_south, [tower.center_x(), tower.center_y()])

	def shoot_x(self):
		if self.orientation == 'south':
			return self.center_x() + 9
		else:
			return self.center_x() + 25

	def shoot_y(self):
		if self.orientation == 'south':
			return self.center_y() + 44
		else:
			return self.center_y()

	def center_x(self):
		return self.x_pos - 25

	def center_y(self):
		return self.y_pos - 50

	def shoot_all(self, screen, placed_towers):
		self.ammunition.update(screen, self.fired_spoons)
		if self.tower_delay == 0:
			self.tower_delay = commons.TOWER_DELAY
			for i in range(len(placed_towers)):
				tower = placed_towers[i]
				if tower.orientation == 'south':
					self.fired_spoons.append(spoon.Spoon(tower.shoot_x(), tower.shoot_y(), 'south'))
				else:
					self.fired_spoons.append(spoon.Spoon(tower.shoot_x(), tower.shoot_y(), 'north'))
		else:
			self.tower_delay -= 1
